<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="loginForm" :model="formData" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="formData.password" type="password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { loginAPI } from '@/apis/user'
import { getToken, setToken } from '@/utils/auth'
const FORMDATA_KEY = 'form_KEY'
export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: process.env.NODE_ENV === 'development' ? 'admin' : '',
        password: process.env.NODE_ENV === 'development' ? 'admin123' : '',
        remember: false
      },

      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  namespaced: true,
  state: () => {
    return {
      // 优先从本地取
      token: getToken() || ''
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // 本地存一份
      setToken(token)
    }
  },
  mounted() {
    // 获取本地存储的表单数据
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY)
    if (cacheFormStr) {
      const cacheFormData = JSON.parse(cacheFormStr)
      this.formData.username = cacheFormData.username
      this.formData.password = cacheFormData.password
    }
  },
  methods: {
    async doLogin() {
      this.$refs.loginForm.validate(async valid => {
        console.log(valid)
        if (valid) {
          console.log('登录成功', this.formData)
          const { username, password } = this.formData
          this.$message.success('登录成功')
          try {
            // 假设你使用 Vuex 进行状态管理
            await this.$store.dispatch('user/doLogin', { username, password })
            console.log('登录成功', this.$store.state.user.token)
            // 登录成功后跳转到首页
            this.$router.push('/')
          } catch (error) {
            console.error('登录失败', error)
            alert('登录失败，请查询工作人员, 电话: 1008611')
          }
        } else {
          alert('登录校验失败，请检查输入')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url('~@/assets/login-bg.svg') no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 0 5%;
  flex: 1;
  max-width: 500px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 8px;

  .title {
    padding-bottom: 24px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
    text-align: center;
  }

  ::v-deep(.el-form-item) {
    display: flex;
    margin-bottom: 24px;
    flex-direction: column;
  }

  ::v-deep(.el-form-item-label label) {
    font-size: 16px;
    color: #8b929d;
  }

  ::v-deep(.el-input__inner),
  ::v-deep(.el-input-password__inner) {
    border-radius: 8px;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
